package com.chezhujie.www.service;

import com.chezhujie.www.core.exception.ClassCheckException;
import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjcomments;
import com.chezhujie.www.domain.CzjcommentsPic;
import com.chezhujie.www.domain.Czjlike;
import com.chezhujie.www.persistence.*;
import com.chezhujie.www.utils.MapManage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.*;


/**
 * 评论
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class CzjcommentsService extends BaseService<Czjcomments>{
    @Autowired
    private CzjcommentsMapper czjcommentsMapper;
    @Autowired
    private CzjcommentsVideoMapper czjcommentsVideoMapper;
    @Autowired
    private CzjcommentsPicMapper czjcommentsPicMapper;
    @Autowired
    private CzjuserinfoMapper czjuserinfoMapper;
    @Autowired
    private CzjlikeMapper czjlikeMapper;

    @Autowired
    public void setBaseMapper(CzjcommentsMapper czjcommentsMapper) {
        this.baseMapper = czjcommentsMapper;
    }

    public List<Map<String,Object>> getListUserinfoId(Map<String, Object> param) {
        return czjcommentsMapper.listUserinfoId(param);
    }

    public void checkEntity(Czjcomments czjcomments) throws ClassCheckException {
        if(czjcomments.getCommentsId()!=null&&czjcomments.getCommentsId().length()>19){
            throw new ClassCheckException("评论评论主键不能超过19个字符");
        }
        if(czjcomments.getContent()!=null&&czjcomments.getContent().length()>255){
            throw new ClassCheckException("评论评论内容不能超过255个字符");
        }
        if(czjcomments.getObjectId()!=null&&czjcomments.getObjectId().length()>19){
            throw new ClassCheckException("评论评论对象不能超过19个字符");
        }
    }
    public void checkListEntity(List<Czjcomments> listCzjcomments) throws ClassCheckException {

        for (Czjcomments czjcomments : listCzjcomments) {
            checkEntity(czjcomments);
        }

    }
    @Override
    public void insert(Czjcomments czjcomments) {
        czjcomments.setApproveStatus(0);
        czjcomments.setCreatetime(new Date());
        czjcomments.setDelFlag(0);
        super.insert(czjcomments);
    }

    /**
     * 修改审批状态
     * @param commentsId
     * @param approveStatus
     */
    public void changeApprove(String commentsId, Integer approveStatus) {
        Czjcomments entity = queryById(commentsId);
        entity.setApproveStatus(approveStatus);
        entity.setApproveTime(new Date());
        update(entity);
    }

    @Override
    public Pagination apilist(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) {
        String userinfoId = String.valueOf(params.get("userinfoId"));
        params.put("approveStatus", 1);
        params.put("delFlag", 0);
        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        params.put("userinfoId", null);
        int count = baseMapper.getApiTotalRows(params);
        List<Map<String, Object>> list = baseMapper.getApiList(params);
        pagination.setTotalCount(count);
        if(list.size()==0){
            pagination.setList(list);
            return pagination;
        }
        List<String> listCommentsId = new ArrayList<String>();
        for (Map<String, Object> map : list) {
            listCommentsId.add(String.valueOf(map.get("commentsId")));
        }
        /** 评论用户集合 **/
        List<String> listUserinfoId = new ArrayList<String>();
        for (Map<String, Object> map : list) {
            listUserinfoId.add(String.valueOf(map.get("userinfoId")));
        }
        if(listCommentsId.size()!=0){
            /** 图片集合 **/
            List<Map<String, Object>> listPic = czjcommentsPicMapper.getApiListPic(listCommentsId);
            /** 视频集合 **/
            List<Map<String, Object>> listVid = czjcommentsVideoMapper.getApiListVid(listCommentsId);

            /** 评论用户集合查询 **/
            List<Map<String, Object>> listUserinfo = new ArrayList<Map<String, Object>>();
            if(listUserinfoId.size()!=0){
                listUserinfo = czjuserinfoMapper.getByIn(listUserinfoId);
            }



            Map<Integer, Map<String, Object>> czjuserinfoMap =    MapManage.toGetKeyMapInt("id", listUserinfo);


            Map<String, List<Map<String, Object>>> mlmPic =  MapManage.toGetKeyListMap("commentsId", listPic);
            Map<String, List<Map<String, Object>>> mlmVid =  MapManage.toGetKeyListMap("commentsId", listVid);

            String commentsId ="";
            for (int i = 0; i < list.size(); i++) {
                commentsId=String.valueOf(list.get(i).get("commentsId"));
                /** 往评论里加入图片集合、视频集合、回复集合 */
                list.get(i).put("listPic",toNull(mlmPic.get(commentsId)));
                list.get(i).put("listVid",toNull(mlmVid.get(commentsId)));
                /** 补充评论用户信息 */
                if(list.get(i).get("userinfoId")!=null
                        &&czjuserinfoMap.get(Integer.valueOf(String.valueOf(list.get(i).get("userinfoId"))))!=null){
                    list.get(i).putAll(czjuserinfoMap.get(Integer.valueOf(String.valueOf(list.get(i).get("userinfoId")))));
                }else{
                    list.get(i).put("nickname", "");
                    list.get(i).put("picPath", "");
                }
            }

        }
        if(StringUtils.isNotBlank(userinfoId)){
            //加入点赞业务
            List<String> listuserifcomments = new ArrayList<String>();
            for (String s : listCommentsId) {
                listuserifcomments.add(userinfoId + s);

            }

            List<Czjlike> listlike = czjlikeMapper.getApiListStr(listuserifcomments);
            Map<String, Czjlike> czjlikeMap = new HashMap<String, Czjlike>();
            for (Czjlike czjlike : listlike) {
                czjlikeMap.put(czjlike.getCommentsId(),czjlike);
            }
            for (int i = 0; i < list.size(); i++) {
                if(czjlikeMap.get(list.get(i).get("commentsId"))==null){
                    list.get(i).put("like",0);
                }else{

                    list.get(i).put("like",czjlikeMap.get(list.get(i).get("commentsId")).getYesorno());
                }
            }
        }else{
            for (int i = 0; i < list.size(); i++) {
                list.get(i).put("like",0);
            }
        }

        pagination.setList(list);




        return pagination;
    }
    public List toNull(List list){
        if(list==null){
            return new ArrayList();
        }
        return list;
    }

    public void changeApprove2(String commentsId, Integer approveStatus) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("commentsId", commentsId);
        map.put("approveStatus", approveStatus);
        Czjcomments entity = queryById(commentsId);
        entity.setApproveStatus(approveStatus);
        entity.setApproveTime(new Date());
        update(entity);
        czjcommentsPicMapper.updateApproveStatusByCommentsId(map);
        czjcommentsVideoMapper.updateApproveStatusByCommentsId(map);
    }

    public Pagination apilpklist(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) {
        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = czjcommentsMapper.getApiLpkTotalRows(params);
        List<Map<String, Object>> list = czjcommentsMapper.getApiLpkList(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
    }
}
